package com.scalar.forecast.jobs;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.WorkWeek;
import com.scalar.chiptrack.utils.DateUtil;
import com.scalar.forecast.systemprefs.SystemPreferencesSingleton;
import com.scalar.forecast.commons.Constants;
import com.scalar.forecast.forecastSetup.dao.ForecastSetupDAO;

import java.util.Calendar;
import java.util.Date;

/**
 * User: ssujan
 * Date: Mar 7, 2010
 * Time: 3:52:42 PM
 */
public class SalesForecastSnapshotJob  extends JobHandler {
    private Logger logger = null;

    public SalesForecastSnapshotJob() throws Exception{
        try {
            logger = new Logger(  "./logs", "salesForecastSnapshotJob", "SalesForecastSnapshotJob", "debug", 1024 * 1000, 5 );
        }
        catch( Exception e ) {
            e.printStackTrace();
        }
    }

    // To be implemented by sub classes
    public void execute() throws Exception {
        logger.logDebug("Executing..");
        String fcCycle = SystemPreferencesSingleton.getForecastCycle();
        int freezingPoint = SystemPreferencesSingleton.getFreezingPoint();
        logger.logDebug( "fcCycle="+fcCycle + " : freezingPoint="+ freezingPoint);
        String snapshotName = null;
        boolean exists = true;
        try {
            if(Constants.FC_CYLCE_WEEKLY.equalsIgnoreCase(fcCycle)) {
                WorkWeek workWeek = WorkWeek.getCurrentWorkWeekInstance();
                Date weekStartDate = workWeek.getCurrentWWStartDate();
                int noOfDaysElapsed = DateUtil.getElapsedDays(weekStartDate);
                logger.logDebug("noOfDaysElapsed="+noOfDaysElapsed);
                snapshotName = "WW_" + workWeek.getCurrentWWNumber() + "_" + workWeek.getWorkWeekYear();
                if(noOfDaysElapsed == freezingPoint) {
                    //check whether snapshot is taken or not
                    exists = ForecastSetupDAO.isForecastSnapshotExists(null, snapshotName);
                }
            } else if(Constants.FC_CYLCE_MONTHLY.equalsIgnoreCase(fcCycle)) {
                Calendar cal = Calendar.getInstance();
                int noOfDaysElapsed = cal.get(Calendar.DATE);
                logger.logDebug("noOfDaysElapsed="+noOfDaysElapsed);
                snapshotName = DateUtil.getShortMonthName(cal.get(Calendar.MONTH)) + "_" + cal.get(Calendar.YEAR);
                if(noOfDaysElapsed == freezingPoint) {
                    //check whether snapshot is taken or not
                    exists = ForecastSetupDAO.isForecastSnapshotExists(null, snapshotName);
                }
            }  else if(Constants.FC_CYLCE_QUARTERLY.equalsIgnoreCase(fcCycle)) {
                Calendar cal = Calendar.getInstance();
                Calendar qtrStart = Calendar.getInstance();
                int currentMonth = cal.get(Calendar.MONTH);
                if(currentMonth >= Calendar.JANUARY  && currentMonth <= Calendar.MARCH) {
                    qtrStart.set(Calendar.MONTH, Calendar.JANUARY);
                    qtrStart.set(Calendar.DATE, 1);
                } else if(currentMonth >= Calendar.APRIL  && currentMonth <= Calendar.JUNE) {
                    qtrStart.set(Calendar.MONTH, Calendar.APRIL);
                    qtrStart.set(Calendar.DATE, 1);
                } else if(currentMonth >= Calendar.JULY  && currentMonth <= Calendar.SEPTEMBER) {
                    qtrStart.set(Calendar.MONTH, Calendar.JULY);
                    qtrStart.set(Calendar.DATE, 1);
                } else if(currentMonth >= Calendar.OCTOBER  && currentMonth <= Calendar.DECEMBER) {
                    qtrStart.set(Calendar.OCTOBER, Calendar.APRIL);
                    qtrStart.set(Calendar.DATE, 1);
                }
                int noOfDaysElapsed = DateUtil.getElapsedDays(cal.getTime(), qtrStart.getTime());
                logger.logDebug("noOfDaysElapsed="+noOfDaysElapsed);
                snapshotName = "Qtr_" + DateUtil.getQuarterOfMonth(cal.get(Calendar.MONTH)+1) + "_" + cal.get(Calendar.YEAR);
                if(noOfDaysElapsed == freezingPoint) {
                    //check whether snapshot is taken or not
                    exists = ForecastSetupDAO.isForecastSnapshotExists(null, snapshotName);
                }
            }

            if( !exists ) {
                logger.logDebug("Saving Forecast Snapshot: " + snapshotName );
                Calendar today = Calendar.getInstance();
                ForecastSetupDAO.saveForecastSnapshot(null, today.get(Calendar.MONTH), today.get(Calendar.YEAR), snapshotName);
                logger.logDebug(" saved successfully");
            } else {
                logger.logDebug("Not saved Forecast Snapshot because FreezingPoint is not reached or completed or snapshot '" + snapshotName + "' is already saved");
            }
        } catch (Exception e) {
            logger.logDebug(e.getMessage(),e);
            throw e;
        }
    }
    public static void main( String[] args ) throws Exception {
        new SalesForecastSnapshotJob().execute();
    }
}
